import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { PrismaClient } from '@prisma/client';
import Home from './home/index.js';

const prisma = new PrismaClient();

export const index = async function(req, res, next) {

    const email = decodeURIComponent(req.query.email)

    if(!email || email === 'undefined') {
        const content = ReactDOMServer.renderToString(<Home />);
        return res.status(200).send(content);
    }

    const comments = await prisma.comment.findMany({
        where: {
            email
        }
    })

    const content = ReactDOMServer.renderToString(<Home email={email} comments={comments} />);
    return res.status(200).send(content);
}

